const db = uniCloud.database();
import store from '@/store/index.js';
import {
	pathToBase64,
	base64ToPath
} from 'image-tools'
exports.install = function(Vue, options) {
	Vue.prototype.client = uni.getSystemInfoSync().uniPlatform
	Vue.prototype.user_id = db.getCloudEnv('$cloudEnv_uid')
	Vue.prototype.openImg = function(images, index) {
			uni.previewImage({
				current: index,
				urls: images
			});
		},
		Vue.prototype.gsbytes = function(limit) {
			var size;
			if (limit < 1024) {
				size = limit + "B"
			} else if (1024 <= limit && limit < 1024 * 1024) {
				size = (limit / 1024).toFixed(2) + "KB"
			} else if (1024 * 1024 <= limit && limit < 1024 * 1024 * 1024) {
				size = (limit / (1024 * 1024)).toFixed(2) + "MB"
			} else if (1024 * 1024 * 1024 <= limit) {
				size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
			}
			return size;
		},
		Vue.prototype.uploadFile = async function(path, success) {
				let suffix = path.substring(path.lastIndexOf('.') + 1);
				const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co");
				const uploadFileOptionsRes = await uniCloudStorageExtCo.getUploadFileOptions({
					cloudPath: `image/${Date.now()}${path.split('/').pop()}.${suffix}`,
				});
				const uploadTask = uni.uploadFile({
					...uploadFileOptionsRes.uploadFileOptions,
					filePath: path,
					success: async () => {
						const res = {
							cloudPath: uploadFileOptionsRes.cloudPath,
							fileID: uploadFileOptionsRes.fileID,
							fileURL: uploadFileOptionsRes
								.fileURL,
						};
						await db.collection("imgdatabase").add({
							url:res.fileURL
						})
						success(res.fileURL)
					},
					fail: (err) => {
						success(null);
					}
				});
			},
			Vue.prototype.openUrl = function(url) {
				if (uni.getSystemInfoSync().uniPlatform == 'web') {
					window.open(url)
				} else {
					plus.runtime.openURL(url)
				}
			},
			Vue.prototype.goPage = function(path, params) {
				if (params == null) {
					params = ""
				}
				uni.navigateTo({
					url: '../' + path + '/' + path + params
				});
			},
			Vue.prototype.copy = function(data, msg) {
				if (msg == null) {
					msg = "复制成功"
				}
				uni.setClipboardData({
					data: data,
					showToast: false,
					success() {
						plus.nativeUI.toast(msg)
					}
				});
			},
			Vue.prototype.message = function(msg, icon) {
				if (uni.getSystemInfoSync().uniPlatform == 'web') {
					uni.showToast({
						icon: icon,
						title: msg,
						duration: 2000
					});
				} else {
					plus.nativeUI.toast(msg)
				}
				return new Promise((resolve, reject) => {
					resolve({
						msg: "操作成功",
						code: 200
					});
				})
			}
	Vue.prototype.geticon = function(type, icon) {
			switch (type) {
				case "file":
					return "../../static/filetype/morenwenjianjia.png"
				case "apk":
					return icon
				case "ipa":
					return icon
				case "img":
					return "../../static/filetype/tupian-jpg_png.png"
				case "video":
					return "../../static/filetype/shipin-mov_mp4_avi.png"
				case "other":
					return icon
				case "document":
					return "../../static/filetype/wendang-docx_doc.png"
			}
		},
		Vue.prototype.getUrl = function(url, fun) {
			uni.request({
				url: `https://lanzou.x2.ink/getLanzouUrl.php?url=${url}`,
				method: "GET",
				success: fun
			})
		}
	Vue.prototype.randomString = function(randomLen, min, max) {
		var str = "",
			pos, str,
			range = min,
			arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
				'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
				'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
				'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
				'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
			];
		if (randomLen) {
			range = Math.round(Math.random() * (max - min)) + min;
		}
		for (var i = 0; i < range; i++) {
			pos = Math.round(Math.random() * (arr.length - 1));
			str += arr[pos];
		}
		return str;
	}
	Vue.prototype.getUserInfo = function() {
		return new Promise((resolve)=>{
			db.collection("uni-id-users").where('_id==$cloudEnv_uid').field({
				avatar: true,
				email: true,
				register_date: true,
				nickname: true,
				username: true,
				state: true,
				vip: true,
				uid: true,
				tel: true,
				identity: true,
				point: true,
				lanzou: true,
				reason: true
			}).get({
				getOne: true
			}).then(res => {
				let data=res.result.data
				console.log(res);
				if (data.state == false) {
					uni.showModal({
						title: '温馨提示',
						showCancel: false,
						content: `你的账号已被封禁,封禁原因：\n"${data.reason}"\n请联系管理员解除封禁`,
						success: function(res) {
							uni.reLaunch({
								url: '../login/login'
							});
						}
					});
					return
				}
				store.commit('setUserInfo', data)
				resolve(data)
			})
		})
	}
	Vue.prototype.getApkInfo = function(path, fun) {
		const apkInfo = {}
		const apk = uni.requireNativePlugin('GetApkInfo')
		apk.get(path,
			(ret) => {
				if (ret.ApkName == null) {
					ret.ApkName = name
				}
				if (ret.VersionName == null) {
					ret.VersionName = "未知"
				}
				if (ret.PackageName == null) {
					ret.PackageName = "未知"
				}
				if (ret.Icon == null) {
					apkInfo.icon = "../../static/filetype/apk.png"
					apkInfo.size = ret.size
					apkInfo.title = ret.ApkName
					apkInfo.type = "apk"
					apkInfo.version = ret.VersionName
					apkInfo.package = ret.PackageName
					fun(apkInfo)
				} else {
					base64ToPath(ret.Icon)
						.then(async function(path) {
							apkInfo.icon = path
							apkInfo.size = ret.size
							apkInfo.title = ret.ApkName
							apkInfo.type = "apk"
							apkInfo.weight = 2
							apkInfo.openup = "apk_moren"
							apkInfo.version = ret.VersionName
							apkInfo.package = ret.PackageName
							fun(apkInfo)
						})
				}
			})
	}
	Vue.prototype.uploadFileToLanzou = (path, fun) => {
		let cookie = store.state.cookie;
		try {
			cookie = store.state.userInfo.lanzou.cookie
		} catch (e) {
			cookie = store.state.cookie
		}
		const request = uni.requireNativePlugin("x2Request")
		request.requests({
			data: {
				task: 1,
				folder_id_bb_n: -1,
				upload_file: path
			},
			headers: {
				Cookie: cookie
			},
			url: "https://pc.woozooo.com/html5up.php"
		}, function(res) {
			fun(res)
		})
	}
}